package com.esri.ags.samples.layers.supportClasses
{

import flash.display.BitmapData;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.display.Shape;
import flash.geom.Matrix;

/**
 * Contains pregenerated arrays that can be used as a HeatMap's gradientArray, used by the ArcGISHeatMapLayer to set the heatmap theme.
 */
public class HeatMapGradientDict
{
    /**
     * Defines the value of the <code>type</code> property of a thermal gradient array.
     */
    public static const THERMAL_TYPE:String = "THERMAL";
    /**
     * Defines the value of the <code>type</code> property of a rainbow gradient array.
     */
    public static const RAINBOW_TYPE:String = "RAINBOW";
    /**
     * Defines the value of the <code>type</code> property of a red white blue gradient array.
     */
    public static const RED_WHITE_BLUE_TYPE:String = "RED_WHITE_BLUE";
    /**
     * Defines the value of the <code>type</code> property of a weather radar gradient array.
     */
    public static const WEATHER_RADAR_TYPE:String = "WEATHER_RADAR";
    /**
     * Defines the value of the <code>type</code> property of a custom gradient array.
     */
    public static const CUSTOM_TYPE:String = "CUSTOM";

    private static const THERMAL:Array = [ 0, 167772262, 336396403, 504430711, 672727155, 857605496, 1025311865, 1193542778, 1361445755, 1529480062, 1714226559, 1882326399, 2050229378, 2218264197, 2386232710, 2571044231, 2739013001, 2906982028, 3075081868, 3243050383, 3427796369, 3595765395, 3763734164, 3931768213, 4099736983, 4284614554, 4284745369, 4284876441, 4285007513, 4285138585, 4285334937, 4285466009, 4285597081, 4285728153, 4285924505, 4286055577, 4286186649, 4286317721, 4286514073, 4286645145, 4286776217, 4286907289, 4287103641, 4287234713, 4287365785, 4287496857, 4287693209, 4287824281, 4287955353, 4288086425, 4288283033, 4288348568, 4288414103, 4288545431, 4288610966, 4288742293, 4288807829, 4288938900, 4289004691, 4289135763, 4289201554, 4289332625, 4289398161, 4289529488, 4289595024, 4289726351, 4289791886, 4289922958, 4289988749, 4290119820, 4290185612, 4290316683, 4290382218, 4290513546, 4290579081, 4290710409, 4290776198, 4290841987, 4290907777, 4290973822, 4291039612, 4291105401, 4291171447, 4291237236, 4291303026, 4291369071, 4291434861, 4291500650, 4291566696, 4291632485, 4291698275, 4291764320, 4291830110, 4291895899, 4291961945, 4292027734, 4292093524, 4292159569, 4292225359, 4292291148, 4292422730, 4292422983, 4292489029, 4292489282, 4292555328, 4292621118, 4292621627, 4292687417, 4292753462, 4292753972, 4292819762, 4292885807, 4292886061, 4292952106, 4292952360, 4293018406, 4293084195, 4293084705, 4293150750, 4293216540, 4293217050, 4293282839, 4293348885, 4293349138, 4293415184, 4293481230, 4293481485, 4293481996, 4293547788, 4293548299, 4293614091, 4293614602, 4293614858, 4293680905, 4293681416, 4293747208, 4293747719, 4293747975, 4293814022, 4293814278, 4293880325, 4293880581, 4293881092, 4293947139, 4293947395, 4294013442, 4294013698, 4294014209, 4294080001, 4294080512, 4294146560, 4294146816, 4294147328, 4294213376, 4294213632, 4294214144, 4294280192, 4294280704, 4294280960, 4294347008, 4294347520, 4294347776, 4294413824, 4294414336, 4294480384, 4294480640, 4294481152, 4294547200, 4294547456, 4294547968, 4294614016, 4294614528, 4294614784, 4294680832, 4294681344, 4294747392, 4294747648, 4294747904, 4294748416, 4294748672, 4294749184, 4294749440, 4294749952, 4294750208, 4294750464, 4294750976, 4294751232, 4294751744, 4294752000, 4294752512, 4294752768, 4294753280, 4294753536, 4294753792, 4294754304, 4294754560, 4294755072, 4294755328, 4294755840, 4294756096, 4294756608, 4294756869, 4294757130, 4294757391, 4294757652, 4294757913, 4294758174, 4294758435, 4294758696, 4294758957, 4294759219, 4294759480, 4294759741, 4294760258, 4294760519, 4294760780, 4294761041, 4294826838, 4294827099, 4294827360, 4294827622, 4294827883, 4294828144, 4294828405, 4294828666, 4294829183, 4294829444, 4294829705, 4294829966, 4294830227, 4294830489, 4294830750, 4294831011, 4294831272, 4294897069, 4294897330, 4294897591, 4294897852, 4294898369, 4294898630, 4294898892, 4294899153, 4294899414, 4294899675, 4294899936, 4294900197, 4294900458, 4294900719, 4294900980, 4294901241, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295 ];
    private static const RAINBOW:Array = [ 0, 167772415, 335544575, 503316726, 671088889, 855638261, 1023410423, 1191182584, 1358954741, 1526726902, 1711276277, 1879048438, 2046820599, 2214592757, 2382364918, 2566914293, 2734686453, 2902458614, 3070230773, 3238002934, 3422552309, 3590324469, 3758096630, 3925868788, 4093640949, 4278190326, 4278191862, 4278193398, 4278195190, 4278196726, 4278198518, 4278200054, 4278201590, 4278203382, 4278204918, 4278206710, 4278208246, 4278209782, 4278211574, 4278213110, 4278214902, 4278216438, 4278217974, 4278219766, 4278221302, 4278223094, 4278224630, 4278226166, 4278227958, 4278229494, 4278296822, 4278232053, 4278232821, 4278233588, 4278234356, 4278235124, 4278235891, 4278236659, 4278237426, 4278238194, 4278238962, 4278239729, 4278240497, 4278241264, 4278242032, 4278242800, 4278243567, 4278244335, 4278245102, 4278245870, 4278246638, 4278247405, 4278248173, 4278248940, 4278249708, 4278250732, 4278250722, 4278250969, 4278251215, 4278251462, 4278251452, 4278251699, 4278251945, 4278252192, 4278252183, 4278252429, 4278252676, 4278252922, 4278252913, 4278253159, 4278253406, 4278253652, 4278253643, 4278253890, 4278254136, 4278254383, 4278254373, 4278254620, 4278254866, 4278255113, 4278255360, 4278910720, 4279566080, 4280221440, 4280876800, 4281597696, 4282253056, 4282908416, 4283563776, 4284219136, 4284940032, 4285595392, 4286250752, 4286906112, 4287561472, 4288282368, 4288937728, 4289593088, 4290248448, 4290903808, 4291624704, 4292280064, 4292935424, 4293590784, 4294246144, 4294967040, 4294900736, 4294834432, 4294768384, 4294702080, 4294636032, 4294569728, 4294503680, 4294437376, 4294371328, 4294305024, 4294238976, 4294172672, 4294106624, 4294040320, 4293974272, 4293907968, 4293841920, 4293775616, 4293709568, 4293643264, 4293577216, 4293510912, 4293444864, 4293378560, 4293378048, 4293377536, 4293442560, 4293507584, 4293572608, 4293637632, 4293702656, 4293767680, 4293832704, 4293897728, 4293962752, 4294027776, 4294092800, 4294158080, 4294223104, 4294288128, 4294353152, 4294418176, 4294483200, 4294548224, 4294613248, 4294678272, 4294743296, 4294808320, 4294873344, 4294938624, 4294937088, 4294935552, 4294934016, 4294932480, 4294931200, 4294929664, 4294928128, 4294926592, 4294925312, 4294923776, 4294922240, 4294920704, 4294919424, 4294917888, 4294916352, 4294914816, 4294913536, 4294912000, 4294910464, 4294908928, 4294907648, 4294906112, 4294904576, 4294903040, 4294901760, 4294903045, 4294904330, 4294905615, 4294906900, 4294908185, 4294909470, 4294910755, 4294912040, 4294913325, 4294914867, 4294916152, 4294917437, 4294918722, 4294920007, 4294921292, 4294922577, 4294923862, 4294925147, 4294926432, 4294927974, 4294929259, 4294930544, 4294931829, 4294933114, 4294934399, 4294935684, 4294936969, 4294938254, 4294939539, 4294941081, 4294942366, 4294943651, 4294944936, 4294946221, 4294947506, 4294948791, 4294950076, 4294951361, 4294952646, 4294954188, 4294955473, 4294956758, 4294958043, 4294959328, 4294960613, 4294961898, 4294963183, 4294964468, 4294965753, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295 ];
    private static const RED_WHITE_BLUE:Array = [ 0, 50331903, 100663551, 167772415, 218104063, 285212927, 335544575, 385876223, 452985077, 503316726, 570425591, 620757240, 671088889, 738197753, 788529401, 855638266, 905969914, 956301562, 1023410427, 1073742071, 1140850935, 1191182584, 1241514232, 1308623096, 1358954744, 1426063609, 1476395257, 1526726905, 1593835770, 1644167418, 1711276282, 1761607930, 1811939578, 1879048440, 1929380090, 1996488954, 2046820603, 2097152251, 2164261113, 2214592761, 2281701625, 2332033273, 2382364923, 2449473787, 2499805436, 2566914298, 2617245946, 2667577594, 2734686458, 2785018106, 2852126972, 2902458620, 2952790267, 3019899132, 3070230780, 3137339644, 3187671292, 3238002939, 3305111803, 3355443452, 3422552316, 3472883964, 3523215612, 3590324477, 3640656124, 3707764988, 3758096636, 3808428285, 3875537149, 3925868797, 3992977662, 4043309309, 4093640957, 4160749822, 4211081470, 4278190335, 4278386939, 4278583544, 4278845684, 4279042289, 4279304430, 4279501034, 4279697639, 4279959779, 4280156384, 4280418525, 4280615129, 4280811734, 4281073874, 4281270479, 4281532620, 4281729224, 4281925829, 4282187969, 4282384574, 4282646715, 4282843319, 4283039924, 4283302064, 4283498669, 4283760810, 4283957414, 4284154019, 4284416159, 4284612764, 4284874905, 4285071509, 4285268114, 4285530254, 4285726859, 4285989000, 4286185604, 4286382209, 4286644349, 4286840954, 4287103095, 4287299699, 4287496304, 4287758444, 4287955049, 4288217190, 4288413794, 4288610399, 4288872539, 4289069144, 4289331285, 4289527889, 4289724494, 4289986634, 4290183239, 4290445380, 4290641984, 4290838589, 4291100729, 4291297334, 4291559475, 4291756079, 4291952684, 4292214824, 4292411429, 4292673570, 4292870174, 4293066779, 4293328919, 4293525524, 4293787665, 4293984269, 4294180874, 4294443014, 4294639619, 4294901760, 4294902531, 4294903302, 4294904330, 4294905101, 4294906129, 4294906900, 4294907671, 4294908699, 4294909470, 4294910498, 4294911269, 4294912040, 4294913068, 4294913839, 4294914867, 4294915638, 4294916409, 4294917437, 4294918208, 4294919236, 4294920007, 4294920778, 4294921806, 4294922577, 4294923605, 4294924376, 4294925147, 4294926175, 4294926946, 4294927974, 4294928745, 4294929516, 4294930544, 4294931315, 4294932343, 4294933114, 4294933885, 4294934913, 4294935684, 4294936712, 4294937483, 4294938254, 4294939282, 4294940053, 4294941081, 4294941852, 4294942623, 4294943651, 4294944422, 4294945450, 4294946221, 4294946992, 4294948020, 4294948791, 4294949819, 4294950590, 4294951361, 4294952389, 4294953160, 4294954188, 4294954959, 4294955730, 4294956758, 4294957529, 4294958557, 4294959328, 4294960099, 4294961127, 4294961898, 4294962926, 4294963697, 4294964468, 4294965496, 4294966267, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295, 4294967295 ];
    private static const WEATHER_RADAR:Array = [ 0, 4284274024, 4284207975, 4284141926, 4284141157, 4284075108, 4284009059, 4284008290, 4283942241, 4283876193, 4283809888, 4283809375, 4283743326, 4283677021, 4283676508, 4283610459, 4283544154, 4283543642, 4283477593, 4283411544, 4283345239, 4283344726, 4283278677, 4283212372, 4283211859, 4283145810, 4283079506, 4283013457, 4283012944, 4282946639, 4282880590, 4282880077, 4282813772, 4282747723, 4282747211, 4282681162, 4282614857, 4282548808, 4282548295, 4282481990, 4282415941, 4282415428, 4282349123, 4282283075, 4282217026, 4282216257, 4282150208, 4282084159, 4282083390, 4282017341, 4281951292, 4281950780, 4282082364, 4282279485, 4282476606, 4282673726, 4282870847, 4283002432, 4283199553, 4283396673, 4283593794, 4283790915, 4283988035, 4284119620, 4284316741, 4284513862, 4284710982, 4284908103, 4285105224, 4285236809, 4285433929, 4285631050, 4285828171, 4286025291, 4286156876, 4286353997, 4286551118, 4286748494, 4286945615, 4287142736, 4287274321, 4287471441, 4287668562, 4287865683, 4288062803, 4288259924, 4288391509, 4288588630, 4288785750, 4288982871, 4289179992, 4289311577, 4289508697, 4289705818, 4289902939, 4290100059, 4290297180, 4290428765, 4290625886, 4290823006, 4291020127, 4291217248, 4291414625, 4291414368, 4291479648, 4291479392, 4291544671, 4291544415, 4291609695, 4291609438, 4291674718, 4291674462, 4291739741, 4291739485, 4291804765, 4291804764, 4291870044, 4291869788, 4291935067, 4292000347, 4292000091, 4292065370, 4292065114, 4292130394, 4292130137, 4292195417, 4292195161, 4292260440, 4292260440, 4292325720, 4292325463, 4292390743, 4292390487, 4292455766, 4292455510, 4292520790, 4292586069, 4292585813, 4292651093, 4292650836, 4292716116, 4292716116, 4292781395, 4292781139, 4292846419, 4292846162, 4292911442, 4292911186, 4292976465, 4292976209, 4293041489, 4293041232, 4293106512, 4293172048, 4293105999, 4293040206, 4292974414, 4292908365, 4292908109, 4292842316, 4292776523, 4292710475, 4292710218, 4292644426, 4292578377, 4292512585, 4292446792, 4292446535, 4292380487, 4292314694, 4292248902, 4292248389, 4292182597, 4292116804, 4292051011, 4291984963, 4291984706, 4291918914, 4291852865, 4291787072, 4291786816, 4291721023, 4291654975, 4291589182, 4291523390, 4291522877, 4291457084, 4291391292, 4291325499, 4291324987, 4291259194, 4291193402, 4291127353, 4291127096, 4291061304, 4290995511, 4290929463, 4290863670, 4290863413, 4290797365, 4290731572, 4290665780, 4290665523, 4290599475, 4290533682, 4290467889, 4290401841, 4290401584, 4290335792, 4290269999, 4290203951, 4290203694, 4290137901, 4290071853, 4290006060, 4289940268, 4289940011, 4289873962, 4289808170, 4289742377, 4289741865, 4289676072, 4289610280, 4289544487, 4289543974, 4289478182, 4289412389, 4289346341, 4289280548, 4289280292, 4289214499, 4289148450, 4289082658, 4289082401, 4289016353, 4288950560, 4288884767, 4288818975, 4288818462, 4288752670, 4288686877, 4288620829, 4288620572, 4288554779, 4288488987, 4288422938, 4288357146, 4288356889, 4288290841, 4288225048, 4288159255, 4288158999, 4288092950, 4288027158, 4287961365, 4287961109 ];
    private static const CUSTOM_PALETTE:Array = [];

    /**
     * Returns the appropriate gradient array based upon the <code>type</code> requested.
     *
     * @param type The constant value representing the heatmap theme.
     * @return The gradient array for the givent heatmap theme.
     */
    public static function gradientArray(type:String):Array
    {
        switch (type.toUpperCase())
        {
            case RAINBOW_TYPE:
            {
                return RAINBOW;
            }
            case RED_WHITE_BLUE_TYPE:
            {
                return RED_WHITE_BLUE;
            }
            case THERMAL_TYPE:
            {
                return THERMAL;
            }
            case WEATHER_RADAR_TYPE:
            {
                return WEATHER_RADAR;
            }
            case CUSTOM_TYPE:
            {
                if (CUSTOM_PALETTE.length > 0)
                {
                    return CUSTOM_PALETTE;
                }
                else
                {
                    return RAINBOW;
                }
            }
            default:
            {
                return RAINBOW;
            }
        }
    }

    /**
     * Fills the custom palette with the colors specified
     * in the array, the colors should be arranged left (cooler) to right (warmer).
     * This allows you to then set the <code>ArcGISHeatMapLayer</code>
     * theme to <code>HeatMapGradientDict.CUSTOM_TYPE</code>.
     *
     * <listing version="3.0">
     * var customColors:Array = [ 0x7E81FF, 0x3EFF1C, 0xFFE919, 0xFF5E07, 0xFF0694, 0xFF8684 ];
     * HeatMapGradientDict.fillCustomPaletteMap(customColors);
     * myHeatMapLayer.theme = HeatMapGradientDict.CUSTOM_TYPE;
     * </listing>
     *
     * @param colors The array of colors to be used to fill in the heatmap.
     * @param alphaValue The transparency of the custom palette.
     * @return
     */
    public static function fillCustomPaletteMap(colors:Array, alphaValue:Number = 1):void
    {
        const bitmapData:BitmapData = new BitmapData(256, 2, true, 0);

        const matrix:Matrix = new Matrix();
        matrix.createGradientBox(bitmapData.width, bitmapData.height, 0, 0, 0);

        const shape:Shape = new Shape();
        const g:Graphics = shape.graphics;

        const alphas:Array = [];
        const ratios:Array = [];
        var ratioVal:Number = 0;
        var ratioInc:Number = 255 / colors.length;
        var len:Number = colors.length - 1;
        for (var c:int = 0; c < len; c++)
        {
            alphas.push(alphaValue);
            ratios.push(ratioVal);
            ratioVal += ratioInc;
        }
        alphas.push(alphaValue);
        ratios.push(255);

        g.clear();
        g.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix);
        g.drawRect(0, 0, bitmapData.width, bitmapData.height);
        g.endFill();
        bitmapData.draw(shape);

        for (var i:int = 0; i < 256; i++)
        {
            CUSTOM_PALETTE[i] = bitmapData.getPixel32(i, 0);
        }
        CUSTOM_PALETTE[0] = 0;
        bitmapData.dispose();
    }
}

}
